﻿using System.Collections.Generic;
using System.Web.UI;
using System.Linq;
using Ext.Net;
using KamLib;
using KamLib.Converts;


namespace WebPoligon
{
    public partial class StorePage : Page
    {

        static StorePage() { UserUIL.NewFields(); }

        [DirectMethod]
        public object GetUsers(string action, Dictionary<string, object> extraParams)
        {
            return new[]
            {
                new User(1, "Mike", "Shpion", true),
                new User(2, "Alis", "Shmelis", false),
                new User(3, "Djon", "Pedgo", false),
                new User(4, "Alex", "Sars", false),
                new User(5, "Bill", "Smitt", false),
            };
        }

        static readonly UserRole[] UserRoles = new[]
        {
            new UserRole(1, 1, "Administrator"),
            new UserRole(2, 2, "Moderator"),
            new UserRole(3, 2, "Member 1"),
            new UserRole(4, 3, "Member 1"),
            new UserRole(5, 3, "Member 2"),
            new UserRole(6, 4, "Member 1"),
            new UserRole(7, 4, "Member 2"),
            new UserRole(8, 4, "Member 3"),
            new UserRole(9, 4, "Member 4"),
            new UserRole(10, 5, "Administrator"),
            new UserRole(11, 5, "Member 2"),
            new UserRole(12, 5, "Member 3"),
        };

        [DirectMethod]
        public object GetUserRoles(string action, Dictionary<string, object> extraParams)
        {
            var userId = -1;

            var prms = new StoreRequestParameters(extraParams);
            var filter = prms.Filter;
            if (filter.Length > 0 && filter[0].Property == "UserID")
            {
                userId = filter[0].Value.ToInt();
            }

            return UserRoles.Where(a => a.UserID == userId);
        }

        public new class User
        {
            public User(int id, string firstName, string lastName, bool isAdmin)
            {
                ID = id;
                FirstName = firstName;
                LastName = lastName;
                IsAdmin = isAdmin;
                P = new PClass { P1 = 1, P2 = 2, P3 = 3, P4 = 4 };
            }

            public int ID { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public bool IsAdmin { get; set; }
            public PClass P { get; private set; }


            public class PClass
            {
                public int P1 { get; set; }
                public int P2 { get; set; }
                public int P3 { get; set; }
                public int P4 { get; set; }
            }
        }

        public class UserRole
        {
            public UserRole(int id, int userId, string name)
            {
                ID = id;
                UserID = userId;
                Name = name;
            }

            public int ID { get; set; }
            public int UserID { get; set; }
            public string Name { get; set; }
        }

    }
}